import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "rabbitmqmodel"
import "models"

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    property int tid1: 0
    property int tid2: 0
    RuntimeData {
        id: id_runtime_data
        onRuntime_cell_tempChanged: {
            console.log("id_runtime_data Runtime_cell_temp:",
                        id_runtime_data.runtime_cell_temp.value)
        }
    }
    function getPublishData() {
        var jsonObj = {
            "condition": "",
            "data": [{
                    "account": "guest",
                    "message": "Administrator login",
                    "timestamp": 1636987690
                }],
            "name": "storage.post",
            "resource": "operation.data"
        }
        var data = JSON.stringify(jsonObj)
        return data
    }
    function build_request() {
        var req = {
            "condition": "",
            "name": "storage.get",
            "resource": "operation.data"
        }
        return JSON.stringify(req)
    }

    Row {
        id: id_row
        x: 0
        y: 0
        height: 100
        width: 640
        Button {
            width: 100
            height: 100
            text: "Subscribe1"
            onClicked: {
                tid1 = RabbitmqManager.subscribe("procedure", function (msg) {
                    console.log("==========button1======== subscribe message:",
                                msg)
                })
            }
        }
        Button {
            width: 100
            height: 100
            text: "Subscribe2"
            onClicked: {
                tid2 = RabbitmqManager.subscribe("procedure", function (msg) {
                    console.log("==========button2======== subscribe message:",
                                msg)
                })
            }
        }
        Button {
            width: 100
            height: 100
            text: "unSubscribe1"
            onClicked: {
                RabbitmqManager.unsubscribe(tid1)
            }
        }
        Button {
            width: 100
            height: 100
            text: "unSubscribe2"
            onClicked: {
                RabbitmqManager.unsubscribe(tid2)
            }
        }
        Button {
            width: 100
            height: 100
            text: "promise"
            onClicked: {
                console.log("click promise")
                //                var data = getPublishData()
                var req = build_request()
                RabbitmqManager.request("storage", req).then(function (msg) {
                    console.log("========promise===success", msg)
                }, function (msg) {
                    console.log("========promise===error", msg)
                })
            }
        }
        Button {
            width: 100
            height: 100
            text: "publish msg"
            onClicked: {
                var data = getPublishData()
                RabbitmqManager.publish("storage", data, function (msg) {
                    console.log("======Button get response=============" + msg)
                })
            }
        }
    }
}
